#define _CRT_SECURE_NO_WARNINGS 1
#include"Heap.h"

void HeapInit(HP* php)
{
	assert(php);
	php->a = NULL;
	php->size = 0;
	php->capacity = 0;
}
void HeapDestroy(HP* php)
{

}

void AdjustUp(HPDatatype* a,int chlid)
{
	int parent = (chlid - 1) / 2;
	while (chlid > 0)
	{
		if (a[chlid] < a[parent])
		{
			HPDatatype tmp = a[chlid];
			a[chlid] = a[parent];
			a[parent] = tmp;
			
			chlid = parent;
			parent = (chlid - 1) / 2;
		}
		else
		{
			break;
		}
	}
}

void HeapPush(HP* php, HPDatatype x)
{
	if (php->capacity == php->size)
	{
		int newCapacity = php->capacity == 0 ? 4 : php->capacity * 2;
		HPDatatype* tmp = (HPDatatype*)realloc(php->a, newCapacity * sizeof(HPDatatype));
		if (tmp == NULL)
		{
			perror("mallo fail");
			return;
		}
		php->a = tmp;
		php->capacity = newCapacity;
	}
	php->a[php->size] = x;
	php->size++;

	AdjustUp(php->a, php->size - 1);
}